<?php
/*首页数据*/
namespace Admin\Model;
use Think\Model;
class CityModel extends Model {
    public function getCityData($data = ""){
    	$user = M('user');//用户表
        $order= M('order');//订单表
        $cars = M('cars');//车辆表
        $city = M('city');//城市表
        $admin= M('admin');//管理员表
        $wayuser= M('wayuser');//渠道表
        $orderoperation = M('order_operation');//操作表
        
        $order_arr= $order->where(array('id'=>$data['orderID']))->find();
        
        $add = $city->where(array('id'=>$order_arr['cityid']))->field('name,pid')->find();

        $addpid  = $city->where(array('id'=>$add['pid']))->field('name')->find();

        $userid	 = $user->where(array('id'=>$order_arr['uid']))->field('nickname,tel,waynum')->find();
        $carsid	 = $cars->where(array('carnum'=>$order_arr['carnum']))->field('name,guide_price,carnum')->find();
        // var_dump( $carsid );die;
        $m['orderid'] = array('EQ',$order_arr['orderid']);
        $m['status']  = array('EQ',1);
        $orderoperationid = $orderoperation->where($m)->order('id DESC')->find();//通过记录表查询到办单人员
        // echo $orderoperation->getLastSql();
       	$usernum   =  $admin->where(array('usernum'=>$orderoperationid['usernum']))->find();
       	$wayuserid = $wayuser->where(array('waynum'=>$userid['waynum']))->find();
       	if($wayuserid){
       		$wayuserid['create_time'] = date('Y-m-d H:i:s',$wayuserid['create_time']);
       		if($wayuserid['status'] == "1"){
       			$wayuserid['status'] = "渠道激活";
       		}elseif($wayuserid['status'] =="0"){
       			$wayuserid['status'] = "渠道关闭";
       		}
       	}
        $arr  = array(
        	  'city'        => $add['name'],
        	  'province'    => $addpid['name'],
        	  'nickname'    => $userid['nickname'],
        	  'waynum'      => $userid['waynum'],
        	  'name'        => $carsid['name'],
        	  'guide_price' => $carsid['guide_price'],
        	  'carnum'      => $carsid['carnum'],
        	  'tel'         => $userid['tel'],
        	  'username'    => $usernum['name'],
        	  'usernum'     => $usernum['usernum'],
        	  'wayusername' => $wayuserid['name'],
        	  'wayusertel'  => $wayuserid['tel'],
        	  'wayuserzy'   => $wayuserid['occupation'],
        	  'wayusersj'   => $wayuserid['create_time'],
        	  'status'      => $wayuserid['status'],
        	  'orderid'     => $order_arr['orderid'],
        	);
        // var_dump($wayuserid,'3333333');
        // var_dump($arr,'22222');die;
        return $arr;
    }

    /**
     * 获取城市指定信息
     * $id. 城市ID
     * $type。获取信息类型
     */
    public function GetCityInfo($id,$type,$s=false) {
       $city_mdb = M('city');
       $order_mdb = M('order');
       if($type=='city_carinfo'){
        $car_mdb = M('car');
          $city_data = $car_mdb->where('region_id='.$id)->select();
       }else if($type=="city_orderinfo"){
        if($s){
         $where = 'jdc_order.cityid in ('.$id.')';
         $order = 'field(jdc_order.cityid,'.$id.')';
         $select = 'select';
        }else{
           $where = 'jdc_order.cityid='.$id;
           $order = 'jdc_order.id asc';
           $select = 'find';
        }
          $city_order = $order_mdb->join('join jdc_city c on jdc_order.cityid=c.id')->field('c.name,c.pid')->order($order)->where($where)->$select();
          return $city_order;
       }else if($type=='city_name'){//
          $cityname = $city_mdb->where('id='.$id)->find();
          return $cityname;
       }
    }

    /**
     * 获取省份和城市
     */
    public function CityGetAll($datatype=false,$keys=false,$city=true) {
      $city_mdb = M('city');

      $sheng = $city_mdb->where('type=1')->select();
      if($city){

        $shi = $city_mdb->where('type=2')->select();

        $sheng_data = [];

        $shi_data = [];

        for ($i=0; $i < count($sheng); $i++) { 
          for ($s=0; $s < count($shi); $s++) { 
            if($shi[$s]['pid']==$sheng[$i]['id']){
              if($keys){
                $sheng[$i]['shi'][$shi[$s]['id']] = $shi[$s];
              }else{
                $sheng[$i]['shi'][] = $shi[$s];
              }
            }
          }
          if($keys){
            $sheng_data[$sheng[$i]['id']] = $sheng[$i];
          }
        }

        if($keys){
          $sheng = $sheng_data;
        }
        
      }
      switch ($datatype) {
        case 'json':
          return json_encode($sheng,true);
          break;
        
        default:
          return $sheng;
          break;
      }
      
    }
}